﻿#include "CDragPreviewWidget.h"
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
#include <QVBoxLayout>
#include "Global.h"
#include "CBaseNode.h"
CDragPreviewWidget::CDragPreviewWidget(QString sGuid, QString sCardGuid, QString sDllName, int nDeviceType, QWidget *pWidget, int nIndex ,QWidget *parent)
	: QLabel(parent)
{

	QFile fileCard(":/SmartControl/Qss/card.qss"); // 资源路径
	if (fileCard.open(QFile::ReadOnly))
	{
		m_sCardqssStr = fileCard.readAll();
		fileCard.close();
	}
	QString sMyqss = pWidget->styleSheet();
	QString merged = m_sCardqssStr + "\n" + sMyqss; // 后面的会覆盖前面的相同规则
	pWidget->setAttribute(Qt::WA_StyledBackground, true);
	pWidget->setStyleSheet(merged);
	QString sDll = "";
	m_nRow = pWidget->property("row").toInt();
	m_nCol = pWidget->property("col").toInt();
	m_sGuid = sGuid;
	m_nIndex = nIndex;
	m_sCardGuid = sCardGuid;
	m_sDllName = sDllName;
	m_nDeviceType = nDeviceType;
	m_pWidget = pWidget;
	setScaledContents(true);     // ✅ QLabel自动缩放pixmap
	m_pixmap = pWidget->grab();  // 原始图像
	setTitel();
	setStyleSheet("border: 1px solid gray;");
	setAttribute(Qt::WA_Hover);          // 开启悬停事件
	setMouseTracking(true);              // 启用鼠标跟踪
	setGuid(sGuid);
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	//setStyleSheet("background-color: lightblue; border: 1px solid gray;");
	setContextMenuPolicy(Qt::CustomContextMenu);
	setStyleSheet(" QWidget {background-color: rgba(87, 163, 176, 150); border: 1px solid gray;}"
		"QWidget:hover {background-color: rgba(87, 163, 176, 225); color:rgb(0, 0, 0);}");
}

CDragPreviewWidget::~CDragPreviewWidget()
{
}

void CDragPreviewWidget::setIndex(int nIndex)
{
	m_nIndex = nIndex;
}

void CDragPreviewWidget::setGuid(QString sId)
{
	CBaseNode *pNode = g_pRoot->findNodeById(sId);
	if (pNode != nullptr)
	{
		setToolTip(pNode->getName());
		m_sGuid = sId;
	}
}

void CDragPreviewWidget::mousePressEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton) 
	{
		QDrag *drag = new QDrag(this);
		QMimeData *mimeData = new QMimeData();
		mimeData->setData("guid", m_sGuid.toUtf8());
		mimeData->setData("cardguid", m_sCardGuid.toUtf8());
		mimeData->setData("dllname", m_sDllName.toUtf8());
		mimeData->setData("devicetype", QByteArray::number(m_nDeviceType));
		mimeData->setData("row", QByteArray::number(m_nRow));
		mimeData->setData("col", QByteArray::number(m_nCol));
		mimeData->setData("index", QByteArray::number(m_nIndex));
		drag->setMimeData(mimeData);
		drag->setPixmap(m_pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
		drag->exec(Qt::MoveAction);
	}
}

void CDragPreviewWidget::setTitel()
{
	if (m_sGuid == "")
	{
		QString sDll =" "+g_mapCardWidget[m_sDllName][0].sDllNameCh +"-"+ m_pWidget->property("name").toString() + A2T("         ") + QString::number(m_nRow) + "*" + QString::number(m_nCol);
		QWidget *container = new QWidget(this);
		QVBoxLayout *layout = new QVBoxLayout(container);
		layout->setContentsMargins(0, 0, 0, 0);
		layout->setSpacing(0);
		QLabel *titleLabel = new QLabel(sDll);
		titleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
		titleLabel->setStyleSheet("background: rgba(122,163,200,150); color: black;border: none; ");
		titleLabel->setMinimumHeight(25);
		QLabel *imageLabel = new QLabel();
		imageLabel->setPixmap(m_pixmap);
		imageLabel->setScaledContents(true);
		imageLabel->setFixedSize(480, 240);  // 宽120，高80
		imageLabel->setStyleSheet("border: none; ");
		layout->addWidget(titleLabel);  // 上方标题
		layout->addWidget(imageLabel);  // 下方图片
		//setMinimumSize(50, 30);      // 避免过小
	}
	else
	{
		setPixmap(m_pixmap);         // 初始化设置
		setMinimumSize(50, 30);      // 避免过小
	}
}
